/**
 * 
 */
package com.a.html5nfc.writer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Locale;

import com.google.common.primitives.Bytes;

import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.util.Log;

public class NFCWriter implements INFCWriter {
    public final String NFCWriter = "NFCWriter";

    
    public boolean writeToNFCTag(String input, Tag tag) {
        NdefMessage message = getInputAsNdefMessage(input);
        int size = message.toByteArray().length;

        try {
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) {
                ndef.connect();

                if (!ndef.isWritable()) {
                    Log.e(NFCWriter, "The tag is not writable!");
                    return false;
                }
                if (ndef.getMaxSize() < size) {
                    Log.e(NFCWriter, "The message size exceeds the  tag capacity limit");
                    return false;
                }

                ndef.writeNdefMessage(message);
                Log.e(NFCWriter, "Tag write complete!");
                return true;
            } else {
                NdefFormatable format = NdefFormatable.get(tag);
                if (format != null) {
                    try {
                        format.connect();
                        format.format(message);
                        Log.e(NFCWriter, "Tag write complete!");
                        return true;
                    } catch (Exception e) {
                        Log.e(NFCWriter, "Failed to format tag", e);
                        return false;
                    }
                } else {
                    Log.e(NFCWriter, "Tag doesn't support NDEF.");
                    return false;
                }
            }
        } catch (IOException e) {
            Log.e(NFCWriter, "IO error", e);
        } catch (FormatException e) {
            Log.e(NFCWriter, "format error", e);
        }
        return false;
    }

    private NdefMessage getInputAsNdefMessage(String message) {
//        byte[] textBytes = message.getBytes(Charset.forName("UTF-8"));
//        NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
//                NdefRecord.RTD_TEXT, new byte[0] , textBytes);
//        return new NdefMessage(new NdefRecord[] { textRecord });
        
        try {
//            Locale locale = Locale.US;
//            final byte[] langBytes = locale.getLanguage().getBytes("US_ASCII");
            final byte[] textBytes = message.getBytes("UTF_8");
            final int utfBit = 0;
//            final char status = (char) (utfBit + langBytes.length);
            final byte[] data= textBytes;
            NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                                 NdefRecord.RTD_TEXT, new byte[0], data);
            return new NdefMessage(new NdefRecord[] { record });
        }catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
